<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MQTT消息发布</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            background-color: #f9f9f9;
            border-radius: 5px;
            padding: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
            border: 1px solid #d6e9c6;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
            border: 1px solid #ebccd1;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>MQTT消息发布</h1>
        
        <div class="form-group">
            <label for="topic">主题:</label>
            <input type="text" id="topic" name="topic" value="test/tp5">
        </div>
        
        <div class="form-group">
            <label for="qos">QoS级别:</label>
            <select id="qos" name="qos">
                <option value="0">0 - 至多一次</option>
                <option value="1">1 - 至少一次</option>
                <option value="2">2 - 仅一次</option>
            </select>
        </div>
        
        <div class="form-group">
            <label for="message">消息内容:</label>
            <textarea id="message" name="message" placeholder="请输入要发布的消息内容"></textarea>
        </div>
        
        <button type="button" onclick="publishMessage()">发布消息</button>
        
        <div id="result" class="result"></div>
    </div>
    
    <script>
        function publishMessage() {
            var topic = document.getElementById('topic').value;
            var message = document.getElementById('message').value;
            var qos = document.getElementById('qos').value;
            var resultDiv = document.getElementById('result');
            
            if (!message) {
                resultDiv.className = 'result error';
                resultDiv.style.display = 'block';
                resultDiv.innerHTML = '错误: 消息内容不能为空';
                return;
            }
            
            // 显示加载状态
            resultDiv.className = 'result';
            resultDiv.style.display = 'block';
            resultDiv.innerHTML = '发送中...';
            
            // 发送AJAX请求
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '{:url("mqtt/publish")}', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        var response = JSON.parse(xhr.responseText);
                        if (response.code === 1) {
                            resultDiv.className = 'result success';
                            resultDiv.innerHTML = '成功: ' + response.msg + 
                                '<br>主题: ' + response.data.topic + 
                                '<br>QoS: ' + response.data.qos + 
                                '<br>时间: ' + response.data.time;
                        } else {
                            resultDiv.className = 'result error';
                            resultDiv.innerHTML = '错误: ' + response.msg;
                        }
                    } else {
                        resultDiv.className = 'result error';
                        resultDiv.innerHTML = '错误: 服务器响应异常 (' + xhr.status + ')';
                    }
                }
            };
            
            // 发送数据
            var data = 'topic=' + encodeURIComponent(topic) + 
                      '&message=' + encodeURIComponent(message) + 
                      '&qos=' + encodeURIComponent(qos);
            xhr.send(data);
        }
    </script>
</body>
</html> 